/**
 * Created by JustinChow on 2016/11/3.
 */

"use strict";

const Util       = require("util");

const GameObject = require("./GameObject");
const Vector     = require("../Utils/Vector");

const Configs    = require("../Configs/GameServer.json");

const StaticBulletConfig = require("../Configs/Datas/Bullet.json");

function Buff(staticId) {
    this.staticId = staticId;

    this.staticConfig = this.loadConfig();

    this.type       = this.staticConfig.type       || -1;
    this.holdTime   = this.staticConfig.holdTime   || -1;
    this.delay      = this.staticConfig.delay      || -1;
    this.buffEff    = this.staticConfig.buffEff    || -1;
    this.skillLevel = this.staticConfig.skillLevel || -1;
}

Buff.prototype.loadConfig = function () {
    return {};
};


function Bullet(objectId, owner, position, gameServer, staticId, target) {
    GameObject.apply(this, Array.prototype.slice.call(arguments));

    this.staticId     = staticId || -1;

    this.staticConfig = this.loadConfig();

    this.targetType = this.staticConfig.targetType || -1;   // 1 Self, 2 Ally, 3 Enemy, 4 All
    this.through    = this.staticConfig.through    || -1;   // -1 Not Go Through, 1 Through With Harm, 2 Through With No Harm
    this.bulletType = this.staticConfig.bulletType || -1;   // 0 Cast Via Button, 1 Auto Cast Towards Enemy, 2 Auto Cast Towards Move Direction
    this.boxType    = this.staticConfig.boxType    || -1;
    this.boxSize    = this.staticConfig.boxSize    || 0;

    this.routationOffset   = new Vector(this.staticConfig.routationOffset[0], this.staticConfig.routationOffset[1])   || new Vector();    // Speed With Init Rotation
    this.positionOffset    = new Vector(this.staticConfig.positionOffset[0], this.staticConfig.positionOffset[1])     || new Vector();    // Position Offset Added To Caster Position
    this.bulletFlyDistance = this.staticConfig.bulletFlyDistance || -1;    // Bullet Distance
    this.bulletFlySpeed    = this.staticConfig.bulletFlySpeed    || -1;    // Bullet Speed
    // this.lifeTime          = 0;
    // if (this.bulletFlyDistance > 0 && this.bulletFlySpeed > 0) {
    //     this.lifeTime = this.bulletFlyDistance / this.bulletFlySpeed;
    // }

    this.damageId   = this.staticConfig.damageId || -1;
    this.delay      = this.staticConfig.delay    || -1;

    ///////////////////////////////////////////////////////////////////////////

    this.flyDistance  = 0;

    // If Reach Add Delay Time Then To Add To Screen To Do Move Logic
    this.added        = false;
    // If Reach Max Distance To Be Removed
    this.removed      = false;

    this.position   = position.add(this.positionOffset.x, this.positionOffset.y);
    this.target     = target;

    this.initCollision();
}
Util.inherits(Bullet, GameObject);

module.exports = Bullet;

Bullet.prototype.setPosition = function (x, y) {
    if (x instanceof Vector) {
        this.position.x = x.x + this.positionOffset.x;
        this.position.y = x.y + this.positionOffset.y;
    } else {
        this.position.x = x + this.positionOffset.x;
        this.position.y = y + this.positionOffset.y;
    }
};

Bullet.prototype.sendUpdate = function () {
    return true;
};

Bullet.prototype.onAdd = function () {

};

Bullet.prototype.onRemove = function () {

};

Bullet.prototype.move = function () {
    if (!this.added || this.removed) return;

    this.position.add(this.getBulletSpeedValue());

    // Out Of Range
    if (this.position.x < this.gameServer.config.borderLeft) this.removed = true;
    if (this.position.x > this.gameServer.config.borderRight) this.removed = true;
    if (this.position.y < this.gameServer.config.borderTop) this.removed = true;
    if (this.position.y > this.gameServer.config.borderBottom) this.removed = true;

    // Reach Max Distance
    if (this.flyDistance >= this.bulletFlyDistance) this.removed = true;

    // Reach Destination
    if (this.target instanceof Vector && this.position.distanceTo(this.target) < Configs.collideSpace) this.removed = true;
};

Bullet.prototype.loadConfig = function () {
    if (!StaticBulletConfig[this.staticId]) return {};
    var config = StaticBulletConfig[this.staticId];
    config.throuth = -1;
    return config;
};

/**
 * Calc Speed Value Base On Speed Per Second And Time Tick Rate
 * @returns {number}
 */
Bullet.prototype.getBulletSpeedValue = function () {
    var scale =  this.bulletFlySpeed / (1e3 / this.gameServer.config.ticks / this.gameServer.config.fullTicks);
    return this.owner.speed.clone().scale(scale);
};
